    //
//  RegisterView2.m
//  iFlower
//
//  Created by Hoang Thuc on 15/07/2010.
//  Copyright 2010 iNIL. All rights reserved.
//

#import "RegisterView2.h"
#import "RegisterView3.h"
#import "iFlowerAppDelegate.h"
#import "MyAccountView.h"
#import "LoginView.h"
#import "User.h"
#import "Utility.h"
#import "Request.h"

@implementation RegisterView2
@synthesize username;
@synthesize pass;
@synthesize con_pass;
@synthesize email;
@synthesize con_email;

- (void) contentLoaded:(NSNotification*) notification
{
	NSLog(@"content loaded");
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ContentLoaded" object:nil];
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ContentFailed" object:nil];
	NSDictionary* dictionary = [[[Request sharedRequest] entries] objectAtIndex:0];
	if([[dictionary objectForKey:@"status"] isEqualToString:@"OK"]){
		RegisterView3 * reg = [[RegisterView3 alloc] initWithNibName:@"RegisterView3" bundle:nil];
		[self.navigationController pushViewController:reg animated:YES];
		[reg release];
	}else {
		[Utility showAlert:[dictionary objectForKey:@"message"]];

	}

	[[iFlowerAppDelegate shareDelegate] hideLoadingView];

}
- (void) contentFailed:(NSNotification*) notification
{
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ContentLoaded" object:nil];
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ContentFailed" object:nil];
	NSLog(@"content failed");
	[[iFlowerAppDelegate shareDelegate] hideLoadingView];
	[Utility showAlert:@"Registration failed. Please contact administrator."];
}

- (void)resignResponder
{
	for (UIView* v in [self.view subviews]) {
		if([v isFirstResponder])
			[v resignFirstResponder];
	}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
	[self resignResponder];
	[super touchesBegan:touches withEvent:event];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
	[UIView beginAnimations:nil context:NULL];
	self.view.frame = CGRectMake(self.view.frame.origin.x, 
								 self.view.frame.origin.y - 60, 
								 self.view.frame.size.width,
								 self.view.frame.size.height);
	[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
	[UIView beginAnimations:nil context:NULL];
	self.view.frame = CGRectMake(self.view.frame.origin.x, 
								 self.view.frame.origin.y + 60, 
								 self.view.frame.size.width,
								 self.view.frame.size.height);
	[UIView commitAnimations];
}

-(IBAction) buttonMenu
{
	[self.navigationController popToRootViewControllerAnimated:YES];
}
-(IBAction) buttonMyAccount
{
	if ([[iFlowerAppDelegate shareDelegate] logined]) {
		MyAccountView * account = [[MyAccountView alloc] initWithNibName:@"MyAccountView" bundle:nil];
		[self.navigationController pushViewController:account animated:YES];
		[account release];
	}
	else {
		LoginView * login = [[LoginView alloc] initWithNibName:@"LoginView" bundle:nil];
		[self.navigationController pushViewController:login animated:YES];
		[login release];		
	}
}
-(IBAction) buttonRegister
{
}
-(IBAction)buttonBack:(id)sender
{
	[self.navigationController popViewControllerAnimated:YES];
}
-(IBAction) buttonSubmit
{
	if([self.username.text length]==0)
	{
		[Utility showAlert:@"Please enter username"];
		[self.username becomeFirstResponder];
		return;
	}
	if([self.pass.text length]==0)
	{
		[Utility showAlert:@"Please enter password"];
		[self.pass becomeFirstResponder];
		return;
	}
	if(![self.pass.text isEqualToString:self.con_pass.text])
	{
		[Utility showAlert:@"Password is not match"];
		[self.pass becomeFirstResponder];
		return;
	}
	if([self.email.text length]==0)
	{
		[Utility showAlert:@"Please enter password"];
		[self.email becomeFirstResponder];
		return;
	}
	if(![self.email.text isEqualToString:self.con_email.text])
	{
		[Utility showAlert:@"Email is not match"];
		[self.email becomeFirstResponder];
		return;
	}
	User* newUser = [[iFlowerAppDelegate shareDelegate] newAccount]; 
	newUser.username = username.text;
	newUser.password = pass.text;
	newUser.email = email.text;
	[[iFlowerAppDelegate shareDelegate] showLoadingView];
	
	[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(contentLoaded:) name: @"ContentLoaded" object: nil];
	[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(contentFailed:) name: @"ContentFailed" object: nil];
	
	[[Request sharedRequest] registerUser:newUser];
	
}

- (void) applicationWillResignActive:(NSNotification*) notification
{
	[[NSNotificationCenter defaultCenter] removeObserver:self name:@"applicationWillResignActive" object:nil];
	[self resignResponder];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(applicationWillResignActive:) name: @"applicationWillResignActive" object: nil];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
